package com.sdp.wx.controller;


import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_repair_team_worker.entity.WxRepairTeamWorker;
import com.sdp.wx.db.wx_repair_team_worker.service.IWxRepairTeamWorkerService;
import com.sdp.wx.db.wx_repair_team_worker.service.impl.WxRepairTeamWorkerServiceImpl;
import com.sdp.wx.view.WxRepairTeamWorkerView;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-02-18
 */
@RestController
@RequestMapping("/WxRepairTeamWorkerController")
public class WxRepairTeamWorkerController {
	
	@Autowired
	private WxRepairTeamWorkerServiceImpl wxRepairTeamWorkerServiceImpl;
	
	@Autowired
	private IWxRepairTeamWorkerService iWxRepairTeamWorkerService;
	
	@Autowired
	private Query query;

	@PostMapping(value = "/viewPage")
	public Result<?> viewPage(@RequestBody DataCenter dataCenter) {
		QueryWrapper<WxRepairTeamWorkerView> queryWrapper = new QueryWrapper<>();
		return Result.ok(query.viewPage(wxRepairTeamWorkerServiceImpl, WxRepairTeamWorkerView.class, dataCenter, queryWrapper));
	}
	
	@PostMapping(value = "/viewList")
	public Result<?> viewList(@RequestBody DataCenter dataCenter) {
		QueryWrapper<WxRepairTeamWorkerView> queryWrapper = new QueryWrapper<>();
		return Result.ok(query.viewList(wxRepairTeamWorkerServiceImpl, WxRepairTeamWorkerView.class, dataCenter, queryWrapper));
	}
	

	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public synchronized Result<?> list(@RequestBody JSONObject obj) {
		if(obj==null){
			return Result.error("人员信息不存在");
		}
		List<WxRepairTeamWorker> workers = JSONArray.parseArray(JSON.toJSONString(obj.get("empList")), WxRepairTeamWorker.class);
		for(WxRepairTeamWorker worker : workers) {
			worker.setKeyId(null);
		}
		String teamId = obj.getString("teamId");
		if(!StringUtils.isEmpty(teamId) && workers!=null && workers.size()>0) {
			List<WxRepairTeamWorker> workerList = iWxRepairTeamWorkerService.queryWorkerByTeamId(teamId);
			if(workerList==null || workerList.size()==0) {
				iWxRepairTeamWorkerService.saveBatch(workers);
			}else {
				
				workerList.addAll(workers);
				iWxRepairTeamWorkerService.saveOrUpdateBatch(workerList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(WxRepairTeamWorker :: getEmpNo))), ArrayList::new)));
			}

		}
		return Result.ok();
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId,@RequestParam(name="orgNo",required=true) String orgNo) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		//删除人员
		iWxRepairTeamWorkerService.removeById(keyId);
		return Result.ok();
	}
	
}

